package com.fzy.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Base64;

/**
 * 下载工具类
 *
 * @author 冯赵杨
 * @create 2018/8/22
 */
public class DownLoadUtil {
    private DownLoadUtil() {
    }

    public static final String EDGE = "EDGE";
    public static final String CHROME = "CHROME";
    public static final String FIREFOX = "FIREFOX";
    public static final String TRIDENT = "TRIDENT";
    public static final String SAFARI = "SAFARI";

    /**
     * 下载resources/templates/下的文件
     *
     * @param ins      文件路径
     * @param fileName 自定义文件名
     * @param request
     * @param response
     * @return
     * @author 冯赵杨
     * @create 2018/8/22
     */
    public static void hrefDownload(InputStream ins, String fileName, HttpServletRequest request, HttpServletResponse response) {
        BufferedInputStream fis = null;
        BufferedOutputStream toClient = null;
        try {

            fis = new BufferedInputStream(ins);
            response.reset();

            String userAgent = request.getHeader("User-Agent");
            fileName = getDownfileName(fileName, userAgent);

            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            //response.addHeader("Content-Length", "" + file.length());
            response.setContentType("application/octet-stream");
            toClient = new BufferedOutputStream(response.getOutputStream());

            byte[] buffer = new byte[8192];
            int i;
            while ((i = fis.read(buffer)) != -1) {
                toClient.write(buffer, 0, i);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (toClient != null) {
                    toClient.flush();
                    toClient.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }


    /**
     * 兼容各个浏览器文件下载
     *
     * @param fileName
     * @param userAgent
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getDownfileName(String fileName, String userAgent) throws UnsupportedEncodingException {
        if (org.apache.commons.lang3.StringUtils.isBlank(userAgent)) {
            return fileName;
        }
        userAgent = userAgent.toUpperCase();
        if (userAgent.contains(CHROME)) {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } else if (userAgent.contains(EDGE)) {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            fileName = fileName.replace("+", "%20");
        } else if (userAgent.contains(FIREFOX)) {
            fileName = "=?utf-8?B?"
                    + Base64.getEncoder().encodeToString(fileName.getBytes("utf-8")) + "?=";
        } else if (userAgent.contains(TRIDENT)) {
            fileName = URLEncoder.encode(fileName, "UTF-8");
            fileName = fileName.replace("+", "%20");
        } else if (userAgent.contains(SAFARI)) {
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
            fileName = fileName.replaceAll("\\+", "%20");
        } else {
            fileName = new String(fileName.getBytes("gbk"), "ISO8859-1");
        }
        return fileName;
    }
}
